package com.gufe.mapper.user.Exhibition;

import com.gufe.entity.Exhibitions.Exhibition;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;

@Mapper
public interface ExhibitionMapper {

    @Select("SELECT * FROM exhibitions")
    List<Exhibition> findAll();

    @Select("SELECT * FROM exhibitions WHERE id = #{id}")
    Optional<Exhibition> findById(Integer id);

    @Insert("INSERT INTO exhibitions (name, start_date, end_date, capacity, reserved_count, status) " +
            "VALUES (#{name}, #{startDate}, #{endDate}, #{capacity}, #{reservedCount}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Exhibition exhibition);

    @Update("UPDATE exhibitions SET name = #{name}, start_date = #{startDate}, end_date = #{endDate}, " +
            "capacity = #{capacity}, reserved_count = #{reservedCount}, status = #{status} WHERE id = #{id}")
    int update(Exhibition exhibition);

    @Update("UPDATE exhibitions SET reserved_count = reserved_count + 1 WHERE id = #{id}")
    int incrementReservedCount(Integer id);

    @Update("UPDATE exhibitions SET reserved_count = reserved_count - 1 WHERE id = #{id} AND reserved_count > 0")
    int decrementReservedCount(Integer id);
}
